package com.example.farmer.Database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.farmer.Entity.CartItem;

import java.util.ArrayList;
import java.util.List;

public class CartDatabase extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "car.db";
    private static final int DATABASE_VERSION = 1;
    private static final String CART_TABLE_NAME = "car_items";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_PRODUCT_ID = "product_id";
    private static final String COLUMN_PRODUCT_NAME = "product_name";
    private static final String COLUMN_PRICE = "price";
    private static final String COLUMN_QUANTITY = "quantity";
    private static final String COLUMN_IMAGE_URI = "product_image";

    private static CartDatabase instance;

    public CartDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static synchronized CartDatabase getInstance(Context context) {
        if (instance == null) {
            instance = new CartDatabase(context);
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CART_TABLE = "CREATE TABLE " + CART_TABLE_NAME + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_PRODUCT_ID + " INTEGER,"
                + COLUMN_PRODUCT_NAME + " TEXT,"
                + COLUMN_PRICE + " REAL,"
                + COLUMN_QUANTITY + " INTEGER,"
                + COLUMN_IMAGE_URI + " TEXT"
                + ")";
        db.execSQL(CREATE_CART_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + CART_TABLE_NAME);
        onCreate(db);
    }

    // 添加商品到购物车的实现
    public void addCartItem(CartItem item) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCT_ID, item.getProductId());
        values.put(COLUMN_PRODUCT_NAME, item.getProductName());
        values.put(COLUMN_PRICE, item.getPrice());
        values.put(COLUMN_QUANTITY, item.getQuantity());
        values.put(COLUMN_IMAGE_URI, item.getProductImage());
        db.insert(CART_TABLE_NAME, null, values);
        db.close();
    }

    // 其他数据库操作方法省略

    @SuppressLint("Range")
    public List<CartItem> getAllCartItems() {
        SQLiteDatabase db = this.getReadableDatabase();
        List<CartItem> cartItems = new ArrayList<>();
        String SELECT_ALL_CART_ITEMS = "SELECT * FROM " + CART_TABLE_NAME;
        try (Cursor cursor = db.rawQuery(SELECT_ALL_CART_ITEMS, null)) {
            while (cursor.moveToNext()) {
                CartItem cartItem = new CartItem();
                // 假设 CartItem 有相应的 setter 方法
                cartItem.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                cartItem.setProductId(cursor.getInt(cursor.getColumnIndex(COLUMN_PRODUCT_ID)));
                cartItem.setProductName(cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_NAME)));
                cartItem.setPrice(cursor.getDouble(cursor.getColumnIndex(COLUMN_PRICE)));
                cartItem.setQuantity(cursor.getInt(cursor.getColumnIndex(COLUMN_QUANTITY)));
                cartItem.setProductImage(cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE_URI)));
                cartItems.add(cartItem);
            }
        }


        return cartItems;
    }



    //根据商品id查询
    @SuppressLint("Range")
    public CartItem getCartItemById(int productId) {
        SQLiteDatabase db = this.getReadableDatabase();
        String SELECT_CART_ITEM_BY_ID = "SELECT * FROM " + CART_TABLE_NAME
                + " WHERE " + COLUMN_PRODUCT_ID + " = ?";
        String[] selectionArgs = {String.valueOf(productId)};

        CartItem cartItem = null;

        try (Cursor cursor = db.rawQuery(SELECT_CART_ITEM_BY_ID, selectionArgs)) {
            if (cursor.moveToFirst()) {
                cartItem = new CartItem();
                cartItem.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                cartItem.setProductId(cursor.getInt(cursor.getColumnIndex(COLUMN_PRODUCT_ID)));
                cartItem.setProductName(cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_NAME)));
                cartItem.setPrice(cursor.getDouble(cursor.getColumnIndex(COLUMN_PRICE)));
                cartItem.setQuantity(cursor.getInt(cursor.getColumnIndex(COLUMN_QUANTITY)));
                cartItem.setProductImage(cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE_URI)));
            }
        }

        db.close();
        return cartItem;
    }

    //更新特定数据
    public int updateCartItem(CartItem item) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCT_NAME, item.getProductName());
        values.put(COLUMN_PRICE, item.getPrice());
        values.put(COLUMN_QUANTITY, item.getQuantity());
        values.put(COLUMN_IMAGE_URI, item.getProductImage());

        // 更新特定 ID 的记录
        int rowsAffected = db.update(CART_TABLE_NAME, values, COLUMN_ID + " = ?",
                new String[] { String.valueOf(item.getId()) });
        db.close();
        return rowsAffected;
    }

    //删除数据
    public void deleteCartItem(int itemId) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(CART_TABLE_NAME, COLUMN_ID + " = ?", new String[] { String.valueOf(itemId) });
        db.close();
    }



}
